package com.judian.smart.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.judian.smart.R;
import com.judian.smart.holder.GateWayViewHolder;
import com.judian.smart.model.GateWay;

import java.util.List;

/**
 * author: Peter
 * date: 2019/08/10 星期六 16:58
 * desc:
 */
public class GateWayAdapter extends RecyclerView.Adapter<GateWayViewHolder> {
    private Context mContext;
    private List<GateWay> mList;

    public GateWayAdapter(Context context, List<GateWay> list) {
        mContext = context;
        mList = list;
    }

    @NonNull
    @Override
    public GateWayViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new GateWayViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_gateway_list, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull GateWayViewHolder holder, int position) {
        GateWay gateWay = mList.get(position);
        if (gateWay != null) {
            if (gateWay.getId() == -1) {
                holder.nameGateWay.setText(gateWay.getGatewayName());
                holder.nameGateWay.setTextColor(Color.RED);
                holder.gatewayStatusTv.setVisibility(View.GONE);
            } else {
                holder.nameGateWay.setText(gateWay.getGatewayName() + "(" + gateWay.getGatewayID() + ")");
                holder.gatewayStatusTv.setText((gateWay.getStateCode() == 100 ? "设备在线" : "设备不在线") + "(" + gateWay.getData1() + ")");
                holder.nameGateWay.setTextColor(Color.WHITE);
                holder.gatewayStatusTv.setVisibility(View.VISIBLE);
            }
        } else {
            holder.nameGateWay.setText("无网关设备在线！");
            holder.nameGateWay.setTextColor(Color.RED);
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }
}
